package frontend;
import java.io.IOException;
import java.util.HashMap;

import backend.Answer;
import backend.Element;
import backend.PDreamSolver;
import backend.ParserException;
import backend.Position;
import backend.Wall;


public class Game {
	/* This variables get the arguments values after calling valdateArgs */
	private static String fileName, method;
	private static int minutes = 0;
	private static boolean progress = false;
	
	public static void main(String[] args) throws IOException, ParserException, InterruptedException {

		//validateArgs(args);
		Frame window = new Frame();
		PDreamSolver pds=new PDreamSolver("./levels/level2.txt", window, progress);
		HashMap<Position, Element> map=pds.getMap();
		
		//AGARRARLO DE dps
		
		window.changeSize(pds.getDim().getRow(),pds.getDim().getCol());		
		window.setVisible(true);
		window.drawStart(pds.getStartPos(), pds.getStartDir());
		for( int i=0;i<pds.getDim().getRow();i++){
			for(int j=0;j<pds.getDim().getCol();j++){
				Position p=new Position(i,j);
				if(map.get(p) instanceof Wall){
					window.drawWall(p);
				}				
			}
		}
		
		pds.exact();
		//Answer ans=pds.getBestAnswer();
		//window.drawPath(ans);
		
		
		System.out.println(pds.getBestAnswer().getPipes());
		System.out.println(pds.getBestAnswer().getPipesCant());
		System.out.println(pds.getTime());
		
	}
	
	public static void validateArgs(String[] args){
		/* Arguments validation */
		int argsCount = 0;
		if(args.length < 2 || args.length > 4){
			System.out.println("Cantidad incorrecta de parametros.");
			System.exit(0);
		}
		fileName = args[0];
		argsCount++;
		if(!args[1].equalsIgnoreCase("exact") && !args[1].equalsIgnoreCase("approx")){
			System.out.println("Cantidad incorrecta de parametros.");
			System.exit(0);
		}
		method = args[1].toLowerCase();
		argsCount++;
		if(method.equals("approx")){
			if(args.length < 3){
				System.out.println("Cantidad incorrecta de parametros.");
				System.exit(0);
			}
			try{
				minutes = Integer.parseInt(args[2]);
			}catch(NumberFormatException e){
				System.out.println("Formato de argumentos incorrecto.");
				System.exit(0);
			}
			argsCount++;
			if(minutes <= 0){
				System.out.println("Cantidad incorrecta de minutos");
				System.exit(0);
			}
		}
		if(args.length == (argsCount+1)){
			if(args[argsCount].equals("progress")){
				progress = true;
			}
		}
	}
	
}

